/**
 * 
 */
package io.dora.zk;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

/**
 * @author zhaohongbo
 *
 */
public class ZkSubscribeTest {
	
	private ZooKeeper zk;
	
	public void createInstance() throws Exception {
		zk = new ZooKeeper("localhost:2181", 3000, new Watcher() {
			public void process(WatchedEvent event) {
				System.out.println("==============================================");
			}
		});
	}
	
	public void getData(final String node) throws Exception {
		byte[] b = zk.getData(node, new ZkNodeWatcher(zk, node), null);
		System.out.println("node data:" + new String(b));
	}
	
	public void close() {
		try {
			zk.close();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		zk = null;
	}

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		ZkSubscribeTest zst = new ZkSubscribeTest();
		zst.createInstance();
		Thread.sleep(500);
		zst.getData("/jshop-cx/com.jd.jshop.rpc.face.HelloRpc");
		synchronized (ZkSubscribeTest.class) {
			while (true) {
				try {
					ZkSubscribeTest.class.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		//zst.close();
	}

}
